package com.bijava.test.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;

public class CreateNote_curator {

    // 创建会话
    public static void main(String[] args) throws Exception {


        //不使用fluent编程风格

        RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(1000, 3);

        // 使用fluent编程风格
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2181")
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .retryPolicy(exponentialBackoffRetry)
                .namespace("base")  // 独立的命名空间 /base
                .build();

        client.start();

        System.out.println("会话2创建了");

        // 创建节点
        /*String path = "/lg-test/type";
        String s = client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath(path, "init".getBytes());
        System.out.println("节点递归创建成功，该节点路径" + s);*/

        String path1 = "/lg-test/lock";
        String result = client.create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.EPHEMERAL)
                .forPath(path1);
        System.out.println("临时节点创建成功：" + result);

        //NodeCache nodeCache = new NodeCache(client, path1, false);
        //PathChildrenCache pathChildrenCache = new PathChildrenCache(client, path1, true);
        //基于cache实现监听
        TreeCache treeCache = TreeCache.newBuilder(client, path1).build();
        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
                TreeCacheEvent.Type type = event.getType();
                ChildData data = event.getData();
                switch (type){
                    case NODE_ADDED:
                        System.out.println("节点添加"+data.getPath()+"类型："+type.name());
                        break;
                    case NODE_UPDATED:
                        System.out.println("节点更新"+data.getPath()+"类型："+type.name());
                        break;
                    case NODE_REMOVED:
                        System.out.println("节点删除"+data.getPath()+"类型："+type.name());
                        break;
                }
            }
        });
        treeCache.start();

        //基于watch实现监听
        client.getData().usingWatcher(new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("watch监听器："+watchedEvent);
            }
        }).forPath(path1);

        Thread.sleep(50000);
        client.setData().forPath(path1,"this is lock".getBytes());

        Thread.sleep(100000);
        Stat stat = client.checkExists().forPath(path1);
        System.out.println("临时节点状态："+stat);
    }
}